<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" inline v-show="showSearch">
      <el-form-item label="部门名称" prop="like.name">
        <el-input v-model="searchForm.like.name" placeholder="请输入部门名称" clearable @keyup.enter="search" />
      </el-form-item>
      <el-form-item label="状态" prop="where.status">
        <el-select v-model="searchForm.where.status" placeholder="部门状态" clearable class="w-120px">
          <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="search">搜索</el-button>
        <el-button icon="Refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-8px w-full">
      <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPerm="['system:dept:add']">新增</el-button>
      <el-button type="info" plain icon="Sort" @click="handleExpand">展开/折叠</el-button>
      <right-toolbar v-model:showSearch="showSearch" @refresh="getList" />
    </div>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="listData"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column prop="name" label="部门名称" min-width="120" />
      <el-table-column prop="list_sort" label="排序" align="center" min-width="60" />
      <el-table-column prop="status" label="状态" align="center" min-width="60">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time">
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center" class-name="small-padding">
        <template v-slot="{ row }">
          <el-button link type="primary" icon="Edit" @click="handleEdit(row)" v-hasPerm="['system:dept:edit']"
            >修改</el-button
          >
          <el-button link type="primary" icon="Plus" @click="handleAdd(row)" v-hasPerm="['system:dept:add']"
            >新增</el-button
          >
          <el-button
            v-if="row.id !== rootDept"
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(row)"
            v-hasPerm="['system:dept:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <dept-form :module-name="moduleName" v-model="formShow" :row="formData" @submit="getList" v-if="formShow" />
  </div>
</template>

<script setup name="Dept">
  import { listDeptApi, delDeptApi } from '@/api/system/dept'
  import DeptForm from './form.vue'

  const moduleName = ref('部门')
  const showSearch = ref(true)
  const searchForm = ref({
    isTotal: 1,
    where: {},
    like: {}
  })
  const { proxy } = getCurrentInstance()
  const { sys_normal_disable } = proxy.useDict('sys_normal_disable')
  const rootDept = proxy.useInitLoad('root_dept_id')

  onMounted(() => getList())

  // 查询部门列表
  const loading = ref(true)
  const listData = ref([])
  function getList() {
    loading.value = true
    listDeptApi(searchForm.value).then(res => {
      listData.value = proxy.listToTree(res?.list || [])
      loading.value = false
    })
  }

  // 搜索和重置
  const searchFormRef = useTemplateRef('searchFormRef')
  function search() {
    getList()
  }
  function reset() {
    searchFormRef.value.resetFields()
    search()
  }

  // 新增和编辑
  const formData = ref(null)
  const formShow = ref(false)
  function handleAdd(row) {
    formData.value = { parent_id: row?.id || 0 }
    formShow.value = true
  }
  function handleEdit(row) {
    const editId = row?.id || 0
    if (!editId) return proxy.$modal.msgError('参数缺失')
    formData.value = { id: editId }
    formShow.value = true
  }

  // 展开/折叠操作
  const isExpandAll = ref(true)
  const refreshTable = ref(true)
  function handleExpand() {
    refreshTable.value = false
    isExpandAll.value = !isExpandAll.value
    nextTick(() => {
      refreshTable.value = true
    })
  }

  // 删除按钮操作
  function handleDelete(row) {
    let delIds = row?.id || 0
    if (!delIds) return proxy.$modal.msgError('参数缺失')
    proxy.$modal
      .confirm('是否确认删除' + moduleName.value + '名称为"' + row.name + '"的数据项?')
      .then(() => {
        return delDeptApi(delIds)
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess(res?.msg || '删除成功')
      })
      .catch(() => {})
  }
</script>
